﻿using Kairos.Models;
using Kairos.Properties;
using PagedList;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Kairos.Controllers
{
    public class SearchController : Controller
    {
        private myContext db = new myContext();

        //
        // GET: /Search/Document

        [AccessControl(Roles.All)]
        public ActionResult Document(int? page)
        {
            string search = null;

            int? MetaData_1ID = null;
            int? MetaData_2ID = null;

            if (Request.Cookies.AllKeys.Contains(Utilities.CookieNameDocsSearchstring))
            {
                var cookie = new JavaScriptSerializer().Deserialize<object[]>(Request.Cookies[Utilities.CookieNameDocsSearchstring].Value);

                search = (string)cookie[0];

                MetaData_1ID = (int?)cookie[1];
                MetaData_2ID = (int?)cookie[2];
            }

            ViewBag.Search = search;

            var q = (search == null && MetaData_1ID == null && MetaData_2ID == null)
                ? db.Documents.Take(0)
                : (search != null && search != string.Empty)
                    ? db.SearchDocuments(search)
                    : db.Documents;

            if (MetaData_1ID != null)
                q = q.Where(d => d.MetaData_1ID == MetaData_1ID);

            if (MetaData_2ID != null)
                q = q.Where(d => d.MetaData_2ID == MetaData_2ID);

            ViewBag.IsResult = (search != null || MetaData_1ID != null || MetaData_2ID != null);

            ViewBag.MetaData1 = new SelectList(db.MetaDatas1, "ID", "Nom", MetaData_1ID);
            ViewBag.MetaData2 = new SelectList(db.MetaDatas2, "ID", "Nom", MetaData_2ID);

            // Nugget for easily paging through any IEnumerable/IQueryable in Asp.Net MVC
            // http://github.com/TroyGoode/PagedList

            int pageNumber = (page ?? 1);

            return View(q.OrderBy(o => o.Titre).ToPagedList(pageNumber, Settings.Default.PageSize));
        }

        //
        // GET: /Search/Graphe

        [AccessControl(Roles.All)]
        public ActionResult Graphe(int? page)
        {
            string search = null;

            if (Request.Cookies.AllKeys.Contains(Utilities.CookieNameGraphesSearchstring))
                search = Request.Cookies[Utilities.CookieNameGraphesSearchstring].Value;

            if (search != null)
                ViewBag.Search = search;

            var q = (search == null)
                ? db.Graphes.Take(0)
                : (search !=null && search != string.Empty)
                    ? db.SearchGraphes(search)
                    : db.Graphes;

            // Nugget for easily paging through any IEnumerable/IQueryable in Asp.Net MVC
            // http://github.com/TroyGoode/PagedList

            int pageNumber = (page ?? 1);

            return View(q.OrderBy(o => o.Titre).ToPagedList(pageNumber, Settings.Default.PageSize));
        }
    }
}
